/*!
    \file    main.c
    \brief   running led

    \version 2017-02-10, V1.0.0, firmware for GD32F30x
    \version 2018-10-10, V1.1.0, firmware for GD32F30x
    \version 2018-12-25, V2.0.0, firmware for GD32F30x
    \version 2020-09-30, V2.1.0, firmware for GD32F30x
*/

/*
    Copyright (c) 2020, GigaDevice Semiconductor Inc.

    Redistribution and use in source and binary forms, with or without modification, 
are permitted provided that the following conditions are met:

    1. Redistributions of source code must retain the above copyright notice, this 
       list of conditions and the following disclaimer.
    2. Redistributions in binary form must reproduce the above copyright notice, 
       this list of conditions and the following disclaimer in the documentation 
       and/or other materials provided with the distribution.
    3. Neither the name of the copyright holder nor the names of its contributors 
       may be used to endorse or promote products derived from this software without 
       specific prior written permission.

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 
OF SUCH DAMAGE.
*/

#include "gd32f30x.h"
#include "gd32f303e_eval.h"
#include "systick.h"
#include <stdio.h>
#include <rtthread.h>
#include "adc.h"
#include "drv_adc.h"
#include "bsp_adc.h"


/* 定义线程控制块指针 */
static rt_thread_t thread1_entry = RT_NULL;
static rt_thread_t thread2_entry = RT_NULL;
/*
*************************************************************************
*                             函数声明
*************************************************************************
*/
static void thread1(void* parameter);
static void ADC(void* parameter);




/*>>>>>>全局变量声明<<<<<<<<<*/
rt_uint8_t temperature;
rt_uint8_t vref_value;




/**
  * @brief  主函数
  * @param  无
  * @retval 无
  */
int main(void)
{	
    /* 
	 * 开发板硬件初始化，RTT系统初始化已经在main函数之前完成，
	 * 即在component.c文件中的rtthread_startup()函数中完成了。
	 * 所以在main函数中，只需要创建线程和启动线程即可。
	 */
	
	thread1_entry =                          /* 线程控制块指针 */
   rt_thread_create( "thread1",              /* 线程名字 */
                 thread1,   /* 线程入口函数 */
                  RT_NULL,             /* 线程入口函数参数 */
                     512,                 /* 线程栈大小 */
                    2,                   /* 线程的优先级 */
                    20);                 /* 线程时间片 */
                   
    /* 启动线程，开启调度 */
  if (thread1_entry!= RT_NULL)
      rt_thread_startup(thread1_entry);
    else
        return -1;
					
			thread2_entry =                          /* 线程控制块指针 */
    rt_thread_create( "adc",              /* 线程名字 */
                      ADC,   /* 线程入口函数 */
                      RT_NULL,             /* 线程入口函数参数 */
                      512,                 /* 线程栈大小 */
                      3,                   /* 线程的优先级 */
                      20);                 /* 线程时间片 */
                   
    /* 启动线程，开启调度 */
   if (thread2_entry!= RT_NULL)
        rt_thread_startup(thread2_entry);
    else
        return -1;
				
    
  
}



void GPIO_init()
{
	
//	systick_config();
	 /* enable the LED1 GPIO clock */
    rcu_periph_clock_enable(RCU_GPIOC);
 
    /* configure led GPIO port */ 
    gpio_init(GPIOC, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_1);
    
 //   gpio_bit_reset(GPIOE, GPIO_PIN_6);

  
}

/* 线程 1 的入口函数 */
static void thread1(void *parameter)
{
	
	GPIO_init();
	


while(1)
 {
    
       gpio_bit_set(GPIOC,GPIO_PIN_1);
    //  rt_kprintf("led1 on, count \r\n");
       rt_thread_mdelay(300);

        gpio_bit_reset(GPIOC,GPIO_PIN_1);
      //  rt_kprintf("led1 off\r\n");
       rt_thread_mdelay(300);
 }
   
    
}

MSH_CMD_EXPORT(thread1, led  sample);



 
static void ADC(void* parameter)
{


    /* system clocks configuration */
    rcu_config();
    /* ADC configuration */
    adc_config();
  


    while(1){
        /* ADC software trigger enable */
        adc_software_trigger_enable(ADC0, ADC_INSERTED_CHANNEL);
        /* delay a time in milliseconds */
        rt_thread_delay(2000);
      
        /* value convert */
        temperature = (1.43 - ADC_IDATA0(ADC0)*3.3/4096) * 1000 / 4.3 + 25;
        vref_value = (ADC_IDATA1(ADC0) * 3.3 / 4096);
      
        /* value print */
        rt_kprintf(" the temperature data is %2.0d degrees Celsius\r\n", temperature);
        rt_kprintf(" the reference voltage data is %5.3dV \r\n", vref_value);
        rt_kprintf(" \r\n");
    }


}




